package channel

import (
	"context"

	v1 "caas-platform-payment/api/channel/v1"
	"caas-platform-payment/internal/consts"
	"caas-platform-payment/internal/dao"
	"caas-platform-payment/internal/model/entity"

	"github.com/gogf/gf/v2/errors/gerror"
)

func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	// return nil, gerror.NewCode(gcode.CodeNotImplemented)
	cls := dao.PayChannel.Columns()
	cmd := dao.PayChannel.Ctx(ctx)
	if req.TenantCode != "" {
		cmd = cmd.Where(cls.TenantCode, req.TenantCode)
	}
	if req.ChannelName != "" {
		cmd = cmd.Where(cls.ChannelName, req.ChannelName)
	}
	if req.AppId != "" {
		cmd = cmd.Where(cls.AppId, req.AppId)
	}
	if req.MerchantId != "" {
		cmd = cmd.Where(cls.MerchantId, req.MerchantId)
	}
	res = &v1.ListRes{Data: make([]entity.PayChannel, 0)}
	err = cmd.Limit((req.Page-1)*req.Size, req.Size).ScanAndCount(&res.Data, &res.Total, true)
	if err != nil {
		l.Error(ctx, err)
		return nil, gerror.NewCode(consts.ListFailed)
	}
	return res, nil

}
